package com.anrongtec.zcpt.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.anrongtec.zcpt.JWTApp;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

public class ToolUtils {
	
	private static int seq = 0;

	public static String getSeq() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateStr = sdf.format(new Date());
		seq++;
//		Log.d("ToolUtis", dateStr + String.format("%06d", seq));
		return dateStr + String.format("%06d", seq);
	}
	
	public static String getAppOs() {
		return Build.PRODUCT + ":" + Build.VERSION.RELEASE + ":"
				+ Build.VERSION.SDK_INT;
	}

	/**
	 * @param context
	 * @return 设备IMSI运营商
	 */
	public static String getIMSI(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String IMSI = tm.getSubscriberId();
		return IMSI;
	}

	/**
	 * @param context
	 * @return 设备IMEI序列号
	 */
	public static String getIMEI(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String IMEI = tm.getDeviceId();
		return IMEI;
	}

	/**
	 * @param context
	 *            上下文
	 * @return 设备序列号
	 */
	public static String getDeviceSN(Context context) {
		return Settings.Secure.getString(context.getContentResolver(),
				Settings.Secure.ANDROID_ID);
	}

	/**
	 * @return 设备类型
	 */
	public static String getDeviceType() {
		return Build.HARDWARE;
	}

	/**
	 * @param context
	 *            上下文
	 * @return 网卡mac
	 */
	public static String getMAC(Context context) {
		WifiManager wifi = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}

	/**
	 * @param context
	 *            上下文
	 * @return 当前应用版本
	 */
	public static String getAppVersion(Context context) {
		String appVersion = null;
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			appVersion = pi.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return appVersion;
	}
	/**
	 * @param context
	 *           上下文
	 * @return 当前应用版本编号
	 */
	public static int getAppVersionCode(Context context) {
		int appVersionCode = 0;
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
			appVersionCode = pi.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		
		return appVersionCode;
	}
	
	

	
	
	/**
	 * 获取ip地址
	 * @return
	 */
	public static String getIpAddr() {
		String ipAddr = getWifiIpAddr();
		if (TextUtils.isEmpty(ipAddr)) {
			ipAddr = getGPRSIpAddr();
		}
		return ipAddr;
	}
	
	
	/**
	 *  获取wifi IP地址
	 * @return
	 */
	public static String getWifiIpAddr() {
		try {
			WifiManager wifiManager = (WifiManager) JWTApp.getAppCtx().getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			int ipAddr = wifiInfo.getIpAddress();
			return int2ip(ipAddr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取数据连接时的ip地址
	 * @return
	 */
	public static String getGPRSIpAddr() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface netInterface = en.nextElement();
				for (Enumeration<InetAddress> inetAddrEnum = netInterface.getInetAddresses(); inetAddrEnum.hasMoreElements();) {
					InetAddress inetAddr = inetAddrEnum.nextElement();
					if (!inetAddr.isLoopbackAddress()) {
						return inetAddr.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 
     * 将ip的整数形式转换成ip形式 
     *  
     * @param ipInt 
     * @return 
     */  
    public static String int2ip(int ipInt) {
        StringBuilder sb = new StringBuilder();
        sb.append(ipInt & 0xFF).append(".");  
        sb.append((ipInt >> 8) & 0xFF).append(".");  
        sb.append((ipInt >> 16) & 0xFF).append(".");  
        sb.append((ipInt >> 24) & 0xFF);  
        return sb.toString();  
    }  
	
	
	
	/**
	 * 隐藏软键盘
	 * 
	 * @param activity
	 */
	public static void hideSoftInput(Activity activity) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	public static void hideSoftInput(View view) {
		InputMethodManager imm = (InputMethodManager) JWTApp.getAppCtx().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
	
	public static void showSoftInput(Activity activity) {
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(activity.getWindow().getDecorView(), InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	public static void showSoftInput(View view) {
		InputMethodManager imm = (InputMethodManager) JWTApp.getAppCtx().getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(view, InputMethodManager.HIDE_NOT_ALWAYS);
	}
	
	
	public static float dp2px(float value) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
				JWTApp.getInstance().getResources().getDisplayMetrics());
	}
	
	public static float px2dp(float pxValue) {
		final float scale = JWTApp.getInstance().getResources().getDisplayMetrics().density;
		return pxValue / scale + 0.5f;
	}
	
	public static boolean hasInternet() {
        boolean flag;
        if (((ConnectivityManager) JWTApp.getAppCtx().getSystemService(
                Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null)
            flag = true;
        else
            flag = false;
        return flag;
    }
}
